package com.lzx.adx.report.support.lifecycle;
import cn.hutool.core.lang.ClassScanner;
import com.google.common.collect.Maps;
import com.lzx.adx.report.annnotions.MediaType;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.support.context.v2.CallBackV2Context;
import com.lzx.adx.report.support.context.v2.ReportV2Context;
import com.lzx.adx.report.support.context.v3.CallBackV3Context;
import com.lzx.adx.report.support.context.v3.ReportV3Context;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class HandlerProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        v2BeanRegister(beanFactory);
        v3BeanRegister(beanFactory);
    }
    private static void v2BeanRegister(ConfigurableListableBeanFactory beanFactory) {
        //产品测注入
        Map<String,Class> reportHandlerMap= Maps.newHashMap();
        ClassScanner.scanPackageByAnnotation("com.lzx.adx.report.support.strategy.v2.channel", ProductType.class).forEach(clazz -> {
            String type=clazz.getAnnotation(ProductType.class).value();
            reportHandlerMap.put(type,clazz);
        });
        ReportV2Context handlerReportContext=new ReportV2Context(reportHandlerMap);
        beanFactory.registerSingleton(ReportV2Context.class.getName(),handlerReportContext);
        //媒体测注入
        Map<String,Class> mediaHandlerMap= Maps.newHashMap();
        ClassScanner.scanPackageByAnnotation("com.lzx.adx.report.support.strategy.v2.media", MediaType.class).forEach(clazz -> {
            String type=clazz.getAnnotation(MediaType.class).value();
            mediaHandlerMap.put(type,clazz);
        });
        CallBackV2Context handlerCallBackContext=new CallBackV2Context(mediaHandlerMap);
        beanFactory.registerSingleton(CallBackV2Context.class.getName(),handlerCallBackContext);
    }
    private static void v3BeanRegister(ConfigurableListableBeanFactory beanFactory) {
        //产品测注入
        Map<String,Class> reportHandlerMap= Maps.newHashMap();
        ClassScanner.scanPackageByAnnotation("com.lzx.adx.report.support.strategy.v3.channel", ProductType.class).forEach(clazz -> {
            String type=clazz.getAnnotation(ProductType.class).value();
            reportHandlerMap.put(type,clazz);
        });
        ReportV3Context reportV3Context=new ReportV3Context(reportHandlerMap);
        beanFactory.registerSingleton(ReportV3Context.class.getName(),reportV3Context);
        //媒体测注入
        Map<String,Class> mediaHandlerMap= Maps.newHashMap();
        ClassScanner.scanPackageByAnnotation("com.lzx.adx.report.support.strategy.v3.media", MediaType.class).forEach(clazz -> {
            String type=clazz.getAnnotation(MediaType.class).value();
            mediaHandlerMap.put(type,clazz);
        });
        CallBackV3Context callBackV3Context=new CallBackV3Context(mediaHandlerMap);
        beanFactory.registerSingleton(CallBackV3Context.class.getName(),callBackV3Context);
    }
}